package pqt;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;

import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class Main {

	private JFrame mainFrame;
	private Conexion conexion = Conexion.getInstance();
	private BaseDeDatos db = BaseDeDatos.getInstance();
	private int dni;
	private int balanza;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Main window = new Main();
					window.mainFrame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Main() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		
		mainFrame = new JFrame();
		mainFrame.setBounds(100, 100, 450, 300);
		mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		mainFrame.getContentPane().setLayout(null);
		
		JButton mainBotonIniciar = new JButton("Iniciar");
		mainBotonIniciar.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent tecla) {
				
				if(tecla.getKeyCode() == KeyEvent.VK_ENTER){
					getDni();
					getBalanza();
					System.out.println(dni);
					System.out.println(balanza);
				}
			}
		});
		mainBotonIniciar.addActionListener(new ActionListener() {
			
			public void actionPerformed(ActionEvent arg0) {
				//System.out.println(conexion.getPeso());
				
				
				
				getDni();
				getBalanza();
				System.out.println(dni);
				System.out.println(balanza);
			}
			
		});
		mainBotonIniciar.setBounds(159, 112, 89, 23);
		mainFrame.getContentPane().add(mainBotonIniciar);
		
	}
	
	private void getDni() {
		
		final JDialog dialogoDni = new JDialog();
		JPanel panelDni = new JPanel();
		dialogoDni.setBounds(100, 100, 614, 405);
		dialogoDni.setResizable(false);
		dialogoDni.getContentPane().setLayout(new BorderLayout());
		panelDni.setBorder(new EmptyBorder(5, 5, 5, 5));
		dialogoDni.getContentPane().add(panelDni, BorderLayout.CENTER);
		panelDni.setLayout(null);
		dialogoDni.setModal(true);
		JLabel lblIngreseSuDni = new JLabel("Ingrese su DNI");
		lblIngreseSuDni.setHorizontalAlignment(SwingConstants.CENTER);
		lblIngreseSuDni.setFont(new Font("Tahoma", Font.PLAIN, 40));
		lblIngreseSuDni.setBounds(0, 21, 602, 60);
		panelDni.add(lblIngreseSuDni);
		
		final JTextField textField = new JTextField();
		textField.setFont(new Font("Tahoma", Font.PLAIN, 40));
		textField.setColumns(10);
		textField.setBounds(53, 117, 494, 67);
		panelDni.add(textField);
		
		JButton dniBotonAceptar = new JButton("Aceptar");
		
		dniBotonAceptar.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent tecla) {
				
				if(tecla.getKeyCode() == KeyEvent.VK_ENTER){
					if(true) {
						dni= Integer.parseInt(textField.getText());
						dialogoDni.dispose();
					}
					
					
				}
			}
		});
		
		dniBotonAceptar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
				      Integer.parseInt(textField.getText());
				      dni = Integer.parseInt(textField.getText());
				    } catch (NumberFormatException ev) {
				     getDni();
				    }
				dialogoDni.dispose();
			}
		});
		dniBotonAceptar.setFont(new Font("Tahoma", Font.PLAIN, 40));
		dniBotonAceptar.setBackground(new Color(50, 205, 50));
		dniBotonAceptar.setBounds(216, 257, 184, 73);
		panelDni.add(dniBotonAceptar);
		dialogoDni.setVisible(true);
		
	}//fin metodo getDni();

private void getBalanza() {
		
		final JDialog dialogoBalanza = new JDialog();
		JPanel panelBalanza = new JPanel();
		dialogoBalanza.setBounds(100, 100, 614, 405);
		dialogoBalanza.setResizable(false);
		dialogoBalanza.getContentPane().setLayout(new BorderLayout());
		panelBalanza.setBorder(new EmptyBorder(5, 5, 5, 5));
		dialogoBalanza.getContentPane().add(panelBalanza, BorderLayout.CENTER);
		panelBalanza.setLayout(null);
		dialogoBalanza.setModal(true);
		JLabel lblIngreseSuDni = new JLabel("Ingrese el equipo a ensayar");
		lblIngreseSuDni.setHorizontalAlignment(SwingConstants.CENTER);
		lblIngreseSuDni.setFont(new Font("Tahoma", Font.PLAIN, 40));
		lblIngreseSuDni.setBounds(0, 21, 602, 60);
		panelBalanza.add(lblIngreseSuDni);
		
		final JTextField textField = new JTextField();
		textField.setFont(new Font("Tahoma", Font.PLAIN, 40));
		textField.setColumns(10);
		textField.setBounds(53, 117, 494, 67);
		panelBalanza.add(textField);
		
		JButton balanzaBotonAceptar = new JButton("Aceptar");
		
		textField.addKeyListener(new KeyAdapter() {
			@Override
			public void keyPressed(KeyEvent tecla) {
				
				if(tecla.getKeyCode() == KeyEvent.VK_ENTER){
					try {
					      Integer.parseInt(textField.getText());
					      balanza = Integer.parseInt(textField.getText());
					    } catch (NumberFormatException ev) {
					     getBalanza();
					    }
					dialogoBalanza.dispose();
				}
			}
		});
		
		balanzaBotonAceptar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try {
				      Integer.parseInt(textField.getText());
				      balanza = Integer.parseInt(textField.getText());
				    } catch (NumberFormatException ev) {
				     getBalanza();
				    }
				dialogoBalanza.dispose();
			}
		});
		balanzaBotonAceptar.setFont(new Font("Tahoma", Font.PLAIN, 40));
		balanzaBotonAceptar.setBackground(new Color(50, 205, 50));
		balanzaBotonAceptar.setBounds(216, 257, 184, 73);
		panelBalanza.add(balanzaBotonAceptar);
		dialogoBalanza.setVisible(true);
		
	}//fin metodo getBalanza();
}
